define(['jquery'],function($){
    var domain='/parameter';
    var getUrlParam = function(name){
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }
    var wordwrap = function( str, width, brk, cut ) {

        brk = brk || '\n';
        width = width || 75;
        cut = cut || false;

        if (!str) { return str; }

        var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');

        return str.match( RegExp(regex, 'g') );

    }
    var getString = function(v){
        if (v) {
            return v;
        }
        return "";
    }

    var GetDistance = function( x,  y,  x1,  y1){
        dx = Math.abs(x1 - x);
        dy = Math.abs(y1 - y);
        var dis = Math.sqrt(Math.pow(dx,2)+Math.pow(dy,2));
        dis = Math.round(dis);
        return dis;
    }
    var showDialog = function (url,option) {
        option = option||{};
        if (option.title) {
            $("#dialog-form").dialog("option", "title", option.title);
        }
        if (option.width) {
            $("#dialog-form").dialog("option", "width", option.width);
        }
        if (option.height) {
            $("#dialog-form").dialog("option", "height", option.height);
        }
        $.ajax(domain+'/svg/'+url).then(function(result){
            $("#dialog-form-content").html(result);
            $("#dialog-form").dialog("open");
        }).fail(function(result){
            alert("System Error:" + result);
            console.log(result);
        })
    };
    var closeDialog = function(){
        $("#dialog-form").dialog("close");
    }
    var HF = {
        version:1.0
    }
    HF.getUrl = function(url) {
        if(/\?/.test(url)){
            return url + "&_=" + Math.random();
        }
        return url + "?_=" + Math.random();

    };
    HF.listQuery = function(){
        $("input[name='pageNo']").val(1);
        $('#tableForm').submit();
    }
    HF.listExcelExport = function(){
        var action = $('#tableForm').attr("action");
        $('#tableForm').attr("action",action+"_excel");
        $('#tableForm').submit();
        $('#tableForm').attr("action",action);
    }
    HF.Dialog = function (config) {
        config = config||{}
        this.width = config.width||600;
        this.height = config.height||600;
        this.id = config.id||"myModal";
        this.showDialog = function(url,params,callback) {
            $("#"+id+" .modal-body").load(HF.getUrl(url),params,function(result){
                if(callback&&typeof callback == Function) {
                    callback(result);
                }
                $("#" + id).modal('show');

            }.bind(this));
        };
    }
    HF.showDialog = function (url,config_) {
        var config = config_||{};
        var id = config.id||"myModal";
        var params = config.params||{};

        $("#"+id+" .modal-body").load(HF.getUrl(url),params,function(result,status,xhr){
            debugger;
            if (xhr.status != 200) {
                $("#" + id + " .modal-body").html(xhr.responseText);
                // alert(xhr.responseText);
            }else if(config.callback&&typeof config.callback == Function) {
                config.callback(result);
            }
            $("#" + id).modal('show')

        });
    }

    HF.ajaxFormSubmit = function(formId,afterCall,failCall,innerBeforeCall){
        var name_value = new Array();
        $.each($('#'+formId+' input , #'+formId+' select , #'+formId+' textarea') ,
            function(){
                if($(this).attr('type')!='button' && $(this).attr('type')!='submit' && $(this).attr('type')!='reset'){
                    var _name = $(this).attr('name')||"";
                    if($(this).attr('type')=='radio'){
                        if(!this.checked){
                            return;
                        }
                    }
                    if($(this).attr('type')=='checkbox'){
                        if(!this.checked){
                            return;
                        }
                    }
                    if(_name==""){
                        return;
                    }
                    var _value = $(this).val();
                    if(_value==''){
                        return;
                    }
                    // 取出name与value数组
                    var nodeArr = new Array();
                    nodeArr.push(_name);
                    //去掉空格
                    nodeArr.push($.trim(_value));
                    // 添加到总的数组中
                    name_value.push(nodeArr.join("="));
                }
            }
        );
        // 提交表单
        // 得到数据
        var _data = name_value.join("&");
        // 得到form表单的action
        var _action = $('#'+formId).attr('action');
        $.ajax({
            type: "POST",
            url: encodeURI(_action),
            data : encodeURI(_data),
            success: afterCall,
            error : failCall,
            beforeSend : innerBeforeCall
        });
    };
    HF.ajaxSubmit = function(_action,_data,afterCall,failCall,innerBeforeCall){

        $.ajax({
            type: "POST",
            url: encodeURI(_action),
            data : encodeURI(_data),
            success: afterCall,
            error : failCall,
            beforeSend : innerBeforeCall
        });
    };
    HF.api = {
        success(result,resolve,reject) {
            if (response.data) {
                if (response.data.status == 'ok') {
                    resolve(response.data);
                } else {
                    reject(response.data.message || "异常，请联系管理员");
                }
            } else {
                console.log(response);
                reject("异常，请联系管理员")
            }

        },
        get(resource,params){
            let that  = this;
            return new Promise(function(resolve,reject){
                $.ajax(utils.baseUrl+"api/"+resource,{
                    contentType:'application/json',
                    method: 'post',
                    data:params
                }).then(function (result) {
                    that.success(result,resolve,reject);
                }).fail(function (error) {
                    console.log(error);
                    reject(error);
                })
            })
        }
    }

    HF.showList = function (id,url,params,callback) {
        params = params||{};
        $("#"+id).load(HF.getUrl(url),params,function(result,status,xhr){
            if (xhr.status != 200) {
                $("#" + id ).html(xhr.responseText);
            }else if(callback&&typeof callback == Function) {
                callback(result);
            }
        });
    }

    return {
        domain:domain,
        baseUrl: domain+'/front/parameter/',
        getUrlParam:getUrlParam,
        wordwrap:wordwrap,
        GetDistance:GetDistance,
        closeDialog:closeDialog,
        showDialog:showDialog,
        getString:getString,
        HF:HF
    }
})